from typing import TypedDict

from PySide6.QtWidgets import QApplication,QWidget
from ui_converter import Ui_Form

class MyWindow(QWidget,Ui_Form):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        #用于存储数据的字典
        self.lengthVar = {'米':100,'千米':100000,'厘米':1,'分米':10}
        self.weightVar = {'克':1,'千克':1000,'斤':500}
        self.TypeDict = {'长度':self.lengthVar,'质量':self.weightVar}

        self.dataTypeComboBox.addItems(self.TypeDict.keys())
        self.oneInputComboBox.addItems(self.lengthVar.keys())
        self.twoInputComboBox.addItems(self.lengthVar.keys())

        self.bind()

    def bind(self):
        self.dataTypeComboBox.currentTextChanged.connect(self.typeChanged)
        self.calcBtn.clicked.connect(self.calc)

    def calc(self):
        bigType = self.dataTypeComboBox.currentText()
        value = self.oneInputEditline.text()
        if value=='':
            return

        currentType = self.oneInputComboBox.currentText()
        transType = self.twoInputComboBox.currentText()

        standardization = float(value)*self.TypeDict[bigType][currentType]
        result = standardization/self.TypeDict[bigType][transType]

        self.originDataLabel.setText(f'{value} {currentType} = ')
        self.transDataLabel.setText(f'{result} {transType} ')
        self.twoInputEditline.setText(str(result))

    def typeChanged(self,text):
        self.oneInputComboBox.clear()
        self.twoInputComboBox.clear()
        self.oneInputComboBox.addItems(self.TypeDict[text].keys())
        self.twoInputComboBox.addItems(self.TypeDict[text].keys())


if __name__ == '__main__':
    app = QApplication([])
    my_window = MyWindow()
    my_window.show()
    app.exec()
